package com.mrduan.otgfilemanager.web;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;

import com.orhanobut.logger.Logger;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by snoopy on 2018/5/8.
 */

public class MySocketServer implements Runnable {
    private static final String TAG="MySocketServer";

    private static final String serverIpString = "10.254.202.6";
    private static final int serverListenPort = 8888;
    private static final int connectionMaxLength = 10;

    public static WifiManager wifi;

    private static Context mContext = null;

    static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Logger.d("获取到的值："+msg);
        }
    };

    public static void startListen(Context ctx) {
        Logger.d("+startListen()");
//        cmd.append("- startListen\n");
        mContext = ctx;
        Thread serverSocketListen = new Thread(new MySocketServer());
        serverSocketListen.start();

        Logger.d("-startListen()");
    }

    public static void stopListen() {

    }


    private static class NetState{

        private String intToIp(int ip){
            return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "."
                    + ((ip >> 24) & 0xFF);
        }
        @SuppressLint("WrongConstant")
        public boolean hasInternet() {
            if(wifi.getWifiState() == 3){
                return true;
            }else{
                return false;
            }
        }
        public String GetIPAddress(){
            String ServerIP = intToIp(wifi.getConnectionInfo().getIpAddress());
            return ServerIP;
        }
    }

    @Override
    public void run() {
        try {
            // establish server socket
            int connIndex = 0;
            ServerSocket serverSocket = new ServerSocket(serverListenPort);//, connectionMaxLength, InetAddress.getByName(serverIpString));
            Message.obtain(handler, 0, "- address:" + serverSocket.getLocalSocketAddress() + "\n").sendToTarget();
            Message.obtain(handler, 0, "- IP:" + new NetState().GetIPAddress() + "\n").sendToTarget();
            Logger.d("port:" + serverSocket.getLocalPort());

            while (true) {
                Message.obtain(handler, 0,"- ServerSocket start listene\n").sendToTarget();
                Socket incoming = serverSocket.accept();
                Message.obtain(handler, 0, "- Connected a client!connIndex:" + connIndex + " RemoteSocketAddress:" + String.valueOf(incoming.getRemoteSocketAddress()) + "\n").sendToTarget();
                Logger.d("Connected a client!connIndex:" + connIndex + " RemoteSocketAddress:" + String.valueOf(incoming.getRemoteSocketAddress()));
//                Thread connHandle = new Thread(new ConnectionHandle(mContext, incoming, connIndex));
//                connHandle.start();
                connIndex++;
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
